﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Dao.Interface;
using NHibernate.Criterion;

namespace Dao
{
    public class MobilePageRepository : Repository<MobilePage>, IMobilePageRepository
    {
        /// <summary>
        /// 获取手机页分页集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="delete">是否删除</param>
        /// <returns></returns>
        public IQueryable<MobilePage> LoadAllMobilePageByPage(int userID,int pageIndex, int pageSize, out int pageCount, bool delete)
        {
            var mobilepageList = this.LoadAll().Where(u => u.UserInfo.ID == userID && u.MarkDelete == delete);
            int totalCount = mobilepageList.Count();//总条数
            //获取总页数
            if (totalCount % pageSize == 0)
            {
                pageCount = totalCount / pageSize;
            }
            else
            {
                pageCount = totalCount / pageSize + 1;
            }
            mobilepageList = mobilepageList.OrderByDescending(m => m.CreateTime);
            mobilepageList = mobilepageList.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return mobilepageList;
        }

        /// <summary>
        /// 获取手机页集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="delete">是否删除</param>
        /// <returns></returns>
        public IList<MobilePage> LoadAllMobilePage(object userID, bool delete)
        {
            return this.Session.CreateCriteria(typeof(MobilePage))
                .Add(Restrictions.Eq("MarkDelete", delete))
                .CreateCriteria("UserInfo").Add(Restrictions.Eq("ID", userID))
                .List<MobilePage>();
        }
    }
}
